package com.project.dao.sys;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.project.dao.BaseDao;
import com.project.entity.sys.Department;

/**
 * 部门Dao
 * @author
 */
@Repository
public class DepartmentDao extends BaseDao<Department>{

	/**
	 * 查看所有部门
	 * @return
	 */
	public List<Department> queryList(){
		String hql = " FROM Department ";
		return super.find(hql, null);
	}
	
	/**
	 * 根据ID 和name查询
	 * @return
	 */
	public Department findByIdAndName(Long id,String name){
		String hql = " FROM Department WHERE id != ? AND name = ? ";
		return super.get(hql, id,name);
	}
	
	/**
	 * 根据name查询
	 * @param name
	 * @return
	 */
	public Department findByName(String name){
		String hql = " FROM Department WHERE name = ? ";
		return super.get(hql,name);
	}
	
	/**
	 * 获取同一级的所有部门
	 * @param parentId
	 */
	public List<Department> findByParentId(Long parentId){
		String hql = " FROM Department WHERE parentId = ? ORDER BY code DESC";
		return super.find(hql,parentId);
	}
	
	/**
	 * 根据部门code获取部门
	 * @param code
	 * @return
	 */
	public Department findByCode(String code){
		String hql = " FROM Department WHERE code = ?";
		return super.get(hql,code);
	}
	
	/**
	 * 查询当前部门下的所有部门
	 * @param code
	 * @return
	 */
	public List<Department> queryByCodeList(String code){
		String hql = " FROM Department WHERE code LIKE ? ";
		return super.find(hql, code+'%');
	}
	
	/**
	 * 删除部门
	 * @param code
	 */
	public void deleteDept(String code){
		String sql = "DELETE FROM s_department WHERE code LIKE '"+code+"%'";
		super.getCurrentSession().createSQLQuery(sql).executeUpdate();
	}
}
